<template>
  <div>
    <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-alert show-icon  title="注意：只允许为第三级分类设置相关参数" type="warning" :closable="false"> </el-alert>
        <el-row class="cate_opt">
            <el-col>
                <span> 选择商品分类： </span>
                <el-cascader
                    v-model="cascaderSelArr"
                    :options="cateList"
                    :props="cascaderPros"
                    @change="cascaderHandleChange"></el-cascader>
            </el-col>            
        </el-row>
        <el-tabs v-model="activeName" @tab-click="handleTabClick">
            <el-tab-pane label="动态参数" name="first">
                <el-button type="primary" size="mini" :disabled="isBtnDisable" @click="addParamsClick">添加参数</el-button>
                <el-table :data="dataList" border stripe v-loading="tableLoading">
                    <el-table-column type="expand"></el-table-column>
                    <el-table-column label="#" type="index"></el-table-column>
                    <el-table-column label="参数名称" prop="para_name"></el-table-column>
                    <el-table-column label="操作">
                        <template scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="editParamClick(scope.row)">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="deleteParamClick(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="静态属性" name="second">
                <el-button type="primary" size="mini" :disabled="isBtnDisable" @click="addProClick">添加属性</el-button>
            </el-tab-pane>
        </el-tabs>
    </el-card>
    <el-dialog :title="'添加'+titleText" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
            <el-form-item :label="titleText+'名称'" prop="para_name">
                <el-input v-model="addForm.para_name"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :title="'修改'+titleText" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
            <el-form-item :label="titleText+'名称'" prop="para_name">
                <el-input v-model="editForm.para_name" ></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name: 'Params',
    mixins: [],
    components: {},
    props: {},
    data () {
        return {
            cateList: [],
            cascaderSelArr: [],
            dataList: [],
            addDialogVisible: false,
            editDialogVisible: false,
            activeName: 'first',
            tableLoading : false,
            addForm: {
                para_name:''
            },
            editForm: {
                para_name:''
            },
            addFormRules: {
                para_name: [
                    { required: true, message: '请输入参数名称', trigger: 'blur' },
                    { min:3,max:10,message:"名称在3-10个字符串之间",trigger:'blur'}
                ]
            },
            editFormRules: {
                para_name: [
                    { required: true, message: '请输入参数名称', trigger: 'blur' },
                    { min:3 , max:10 , trigger:'blur' , message:"名称在3-10个字符串之间"}
                ]
            },
            cascaderPros: { expandTrigger: 'hover', label: 'catename', value: 'id', children: 'children' },
        }
    },
    computed: {
        isBtnDisable() {
            if (this.cascaderSelArr.length !== 3) {
                return true
            }
            return false
        },
        cateId() {
            //级联选择器选中的3级分类的id
            if (this.cascaderSelArr.length === 3) {
                return this.cascaderSelArr[2]
            }
            return null
        },
        titleText() {
            if (this.activeName === 'first') {
                return '参数'
            } else {
                return '属性'
            }
        }
    },
    watch: {},
    created() {
        this.getCateAll()
    },
    mounted () {},
    methods: {
        getCateAll() {
            this.http.get('/cates/list',{params:this.param}).then((res) => {
                if (res.data.code === 200) {
                    this.cateList = res.data.data
                }
            }).then((err) => {
                this.tableLoading = false
            })
        },
        cascaderHandleChange() {
            if (this.cascaderSelArr.length !== 3) {
                this.cascaderSelArr = []
                return
            }
            this.tableLoading = true
            this.http.get('/params/list').then((res) => {
                this.tableLoading = false
                if (res.data.code === 200) {
                    this.dataList = res.data.data
                }
            }).catch((err) => {
                this.$message.error(err)
                this.tableLoading = false
            })
        },
        handleTabClick() {
            console.log(this.activeName)
        },
        addParamsClick() {
            this.addDialogVisible = true
        },
        addProClick() {
            this.addDialogVisible = true
        },
        addDialogClose() {
            this.$refs.addFormRef.resetFields()
            this.addDialogVisible = false
        },
        editParamClick(row) {
            this.editDialogVisible = true            
            this.editForm.para_name = row.para_name
        },
        editDialogClose() {
            this.editDialogVisible = false
            this.$refs.editFormRef.resetFields()
        },
        deleteParamClick(row) {
            this.$confirm('确认删除', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'danger'
            }).then(() => {
                this.$message.success('删除成功')
            }).catch(() => {
                this.$message({ type: 'info', message: '已取消删除' });          
            });
        }
    }
}
</script>

<style scoped lang="scss">
.cate_opt{
    margin: 15px 0;
}
</style>
